var BLOCKHTMLSELECTLOOKUP;

function blockhtmlselectlookup(){
	BLOCKHTMLSELECTLOOKUP=1;	
}
function allowhtmlselectlookup(){
	BLOCKHTMLSELECTLOOKUP=0;	
}

function htmlselect(pinput){
	var input=pinput;
	$(input).attr('AUTOCOMPLETE','OFF');
	var local=$(input).attr('local');
	var table=$(input).attr('table');
	var name=$(input).attr('name');
	var oldvalue=$(input).attr('value');
	var lastrequest=-1;
	var hover_id=-1;
	var timerid=0;
	var hovered=0;
	var opened=0;
	input.style.dislay='block';
	var valueholder=0;//az az :input aminek a virtuális mezője az aktuális mezője a this.input ("parent" field)
	var valueholdervalue=0;
	var valueholderoldvalue='';
	var set=0;
	var level=0;
	ctx=$(input).parent();
	while(!set && level<10){//level: hány szintet léphet fel
		ctx=$(ctx).parent();					
		if($('input[name='+local+']',ctx).length){
			valueholder=$('input[name='+local+']',ctx).first();
			set=1;
		};
		level++;
	};
	$(valueholder).attr('AUTOCOMPLETE','OFF');
	$(input).attr('AUTOCOMPLETE','OFF');	

	var optionsdiv=document.createElement('div');
	input.optionsdiv=optionsdiv;
	input.optionsdiv.className='selectoptions';
	input.optionsdiv.id=S4();	
	input.optionsdiv.innerHTML='<img src="/images/uiicons/loading.gif" style="float:right;" />';

	$(valueholder).unbind('focus.htmlselect').bind('focus.htmlselect',function(){
		valueholdervalue=$(this).attr('value');
		valueholderoldvalue=valueholdervalue;
		$(this).unbind('blur.htmlselect').bind('blur.htmlselect',function(){timerid=0;});
		if(document.getElementById(input.optionsdiv.id).valueholderfetch){
			timerid=setInterval('if(document.getElementById("'+input.optionsdiv.id+'"))document.getElementById("'+input.optionsdiv.id+'").valueholderfetch();',1000);
		}else{
			timerid=0;		
		}
	});		
	
	var API={
		valueholderfetch:function(){
			tmpval=$(valueholder).attr('value');
			if(valueholdervalue!=tmpval){
				valueholdervalue=tmpval;
				API.fetchbackward(tmpval);
			}		
		},
		fetch:function(pq){		
			if(!pq)pq=$(input).attr('value');
			$('#'+optionsdiv.id).css('width',$(input).width()-2);
			if(pq==lastrequest)return;
			API.realfetch(pq);
		},
		realfetch:function(pq){
			$('#'+optionsdiv.id).css('width',$(input).width()-2);
			lastrequest=pq;	
			if(!pq)pq='';
			var q=pq.toLowerCase();
			API.open();	
			hovered=0;
			url='/admin.php/data/'+table+'/'+name+'/enum.html?q='+pq;
			var o='<ul>';
			
			$.getJSON(url,function(resp){	
				hover_id=0;
				for(i in resp){
					o+='<li value="'+resp[i].id+'" pos="'+i+'">'+resp[i].name+'</li>';
					if(!hover_id)hover_id=resp[i].id;
				};
				$('#'+input.optionsdiv.id).html(o+'</ul>');
				
				$('#'+input.optionsdiv.id+' li').unbind('click.htmlselect').bind('click.htmlselect',function(){
					API.select(this);
					return false;				
				}).unbind('mouseover.htmlselect').bind('mouseover.htmlselect',function(){
					API.hover(this,1);
				}).unbind('mouseleave.htmlselect').bind('mouseleave.htmlselect',function(){
					
				});
				$('#'+input.optionsdiv.id+' li[value='+hover_id+']').each(function(){
					API.hover(this);
				});
				var y=0;
				$('li',input.optionsdiv).each(function(){
					$(this).attr('y',y);
					y+=parseInt(this.offsetHeight);
				});
				if(y>300)y=300;
				if(y==0){
					//API.close();
				}else{
					$('#'+optionsdiv.id).css('height',y);
				};
			});			
		},
		fetchbackward:function(val){
			url='/common.php/data/'+table+'/'+name+'/enum.html?a='+val;
			$.getJSON(url,0,function(resp){
				if(resp[0] && resp[0].name){
					oldvalue=html_entity_decode(resp[0].name);
					input.value=oldvalue;					
					if($(input).attr('defaultColor'))$(input).css('color',$(input).attr('defaultColor'));
					$(input).trigger('change');
				}
			})
		},
		select:function(li){
			if(hovered){
				modified=0;
				if($(li).attr('value')){
					valueholder.attr('value',li.getAttribute('value'));
					valueholder.attr('changed',1);
					oldvalue=$(li).html();
					modified=1;
				}else{
					oldvalue='';
				}
				$(input).attr('value',oldvalue);
				$(input).trigger('change');
				if(modified){
					$(valueholder).trigger('change');
					$(valueholder).trigger(document.all?'keyup':'keydown');
					if($(input).attr('defaultColor'))$(input).css('color',$(input).attr('defaultColor'));
					if($(valueholder).attr('defaultColor'))$(valueholder).css('color',$(valueholder).attr('defaultColor'));
				};				
				API.close();
				return;
			}else{
				API.close();	
				return;
			}					
		},
		hover:function(li,dontscroll){//-------------------------------------
			if(!opened)return;
			if(!li)return;
			if(li[0])li=li[0];
			if(hovered)$(hovered).removeClass('highlight');
			hovered=li;			
			if(!dontscroll){
				h=li.offsetHeight*1;
				y=$(li).attr('y')*1;	
				vh=parseInt(input.optionsdiv.style.height);
				vmin=input.optionsdiv.scrollTop;
				vmax=vmin+vh;if(y<vmin){
					input.optionsdiv.scrollTop=y;
				}else
				if((y+h)>vmax){
					input.optionsdiv.scrollTop=y-vh+h;					
				}					
			}
			hover_id=$(li).attr('value');
			$(li).addClass('highlight');
		},
		stepdown:function(){
			var cancel=0;
			$('.selectoptions:visible').each(function(){cancel=1;});
			if(!opened && cancel)return;			
			if(!opened)API.fetch($(input).val());
			if(!hovered)return API.hover($('#'+input.optionsdiv.id+' li').first());
			next=$(hovered).next();
			if(!next.length)next=$('#'+input.optionsdiv.id+' li').first();
			API.hover(next,0);
			return false;
		},
		stepup:function(){
			if(!hovered)return API.hover($('#'+input.optionsdiv.id+' li').last());
			previous=$(hovered).prev();
			if(!previous.length)previous=$('#'+input.optionsdiv.id+' li').last();
			API.hover(previous,0);
			return false;
		},
		open:function(){
			if(opened)return;
			$('.selectoptions:visible').each(function(){
				if(this.id!=optionsdiv.id)this.close();//close all OTHER optionsdiv				
			});
			if(timerid==0)timerid=setInterval('if(document.getElementById("'+input.optionsdiv.id+'"))document.getElementById("'+input.optionsdiv.id+'").fetch();',1000);
			hovered=0;
			$(input).removeAttr('readonly');
			input.optionsdiv.style.display='block';
			if(document.all){
				input.optionsdiv.style.marginLeft=-input.clientWidth+'px';				
				input.optionsdiv.style.marginTop=input.clientHeight+'px';				
			}
			$('#'+input.optionsdiv.id).stop().css('height',18);
			$(document).bind('click.htmlselect',function(){API.close();});
			opened=1;
			return false;			
		},
		close:function(){
			//if(!opened)return;
			if(timerid>0)clearInterval(timerid);
			$('.selectoptions:visible').each(function(){
				$(this).hide();
			});
			$(input).attr('value',oldvalue);
			$(input).trigger('change');
			opened=0;
			$(document).unbind('click.htmlselect');
		},
		idchange:function(id){
			alert('idchange('+id+')');
		},
		first:function(){API.hover($('#'+input.optionsdiv.id+' li').first());},
		last:function(){API.hover($('#'+input.optionsdiv.id+' li').last());},
		keyevent:function(e,type){
			if(!opened){
				if(e.keyCode>50)API.fetch($(input).val());
				else
				if(e.keyCode==8)API.fetch($(input).val());//backspace
			}			
			if(timerid<0)timerid=setInterval('if(document.getElementById("'+input.optionsdiv.id+'"))document.getElementById("'+input.optionsdiv.id+'").fetch();',1000);;
			if(e.keyCode==36)return API.first();
			if(e.keyCode==35)return API.last();
			if(e.keyCode==27)return API.close();
			if(e.keyCode==13){API.select(hovered);return false;}
			if(opened!=0 && $.browser.mozilla && type!='keypress' && (e.keyCode==40 || e.keyCode==38)){
				return;
			}
			if(e.keyCode==40)return API.stepdown();
			if(e.keyCode==38)return API.stepup();
		}
	};
	input.optionsdiv.fetch=API.fetch;
	input.optionsdiv.valueholderfetch=API.valueholderfetch;
	input.optionsdiv.close=API.close;
	$(input).after(optionsdiv);	
	optionsdiv.style.display='none';
	$('#'+optionsdiv.id).css('width',$(input).width()-2);
	$(input).bind('keyup.htmlselect',function(e){
		return API.keyevent(e,'keyup');
	}).unbind('keypress.htmlselect').bind('keypress.htmlselect',function(e){
		return API.keyevent(e, 'keypress');
	}).unbind('click.htmlselect').bind('click.htmlselect',function(){
		timerid=-1;
		API.realfetch('');	
		return false;
	}).unbind('keydown.htmlselect').bind('keydown.htmlselect',function(e){
		//if(e.keyCode==38 || e.keyCode==40)return false;
	});
	$(input).addClass('htmlselect');
}

function htmlselectinit(){
	$('input[local]').each(function(){htmlselect(this);});	
}

